﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace MvdDonk.Bomberman.Entities
{
    class Bomb : Entity
    {
        private double explodingTime;
        private List<Vector2> firePositions;
        public int Strength { get; private set; }

        public Bomb(Texture2D sprite, Vector2 position)
            : base(sprite, position)
        {
            TimeToLive = 3000;
            Strength = 2;
        }

        public override void Update(GameTime gameTime, TiledLib.Map map)
        {
            base.Update(gameTime, map);

            if (IsDestroying)
            {
                if (explodingTime == 0)//First time here
                {
                    firePositions = new List<Vector2>();
                    var collisionLayer = (TiledLib.TileLayer)map.GetLayer("Meta");
                    for (var i = 1; i <= Strength; i++)
                    {
                        var mapPoint = map.WorldPointToTileIndex(Position);
                        firePositions.Add(Utils.TileIndexToWorldVector(mapPoint, map));
                        if (mapPoint.X - i * 1 > 0)
                        {
                            var point = new Point(mapPoint.X - i * 1, mapPoint.Y);
                            //There is no collision or its destroyable
                            if (collisionLayer.Tiles[point.X, point.Y] == null || collisionLayer.Tiles[point.X, point.Y].PropertyIsTrue("Destroyable"))
                                firePositions.Add(Utils.TileIndexToWorldVector(point, map));
                        }
                        if (mapPoint.X + i * 1 < map.Width)
                        {
                            var point = new Point(mapPoint.X + i * 1, mapPoint.Y);
                            if (collisionLayer.Tiles[point.X, point.Y] == null || collisionLayer.Tiles[point.X, point.Y].PropertyIsTrue("Destroyable"))
                                firePositions.Add(Utils.TileIndexToWorldVector(point, map));
                        }
                        if (mapPoint.Y - i * 1 > 0)
                        {
                            var point = new Point(mapPoint.X, mapPoint.Y - i * 1);
                            if (collisionLayer.Tiles[point.X, point.Y] == null || collisionLayer.Tiles[point.X, point.Y].PropertyIsTrue("Destroyable"))
                                firePositions.Add(Utils.TileIndexToWorldVector(point, map));
                        }
                        if (mapPoint.Y + i * 1 < map.Height)
                        {
                            var point = new Point(mapPoint.X, mapPoint.Y + i * 1);
                            if (collisionLayer.Tiles[point.X, point.Y] == null || collisionLayer.Tiles[point.X, point.Y].PropertyIsTrue("Destroyable"))
                                firePositions.Add(Utils.TileIndexToWorldVector(point, map));
                        }
                    }
                }
                explodingTime += gameTime.ElapsedGameTime.TotalMilliseconds;

                if (explodingTime > 1000)
                    IsAlive = false;
            }
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            base.Draw(spriteBatch);
            if (firePositions != null)
                firePositions.ForEach(f => spriteBatch.Draw(Bomberman.Instance.DevTexture, f, Color.White));
        }

        public override void Destroy()
        {
            explodingTime = 0;
        }
    }
}
